package com.kotiln.lgc.lib_base.helper

import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager

/**
 *@fileName WindowHelper
 *@date2021/1/27  16:53
 *@author WindowHelper$
 *@describe TODO
 *@desc 窗口帮助类
 */
object WindowHelper {

    private lateinit var mContext: Context
    private lateinit var vm: WindowManager
    private lateinit var lp:WindowManager.LayoutParams

    fun initHelper(mContext: Context) {
        this.mContext = mContext
        vm = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager

       lp= createLayoutParams()
    }

    /**
     * 创建布局属性
     * width:窗口的宽
     * height:窗口的高
     */
    private fun createLayoutParams():WindowManager.LayoutParams {
        val lp = WindowManager.LayoutParams()
        lp.apply {
            this.width = WindowManager.LayoutParams.MATCH_PARENT
            this.height = WindowManager.LayoutParams.MATCH_PARENT
            gravity = Gravity.CENTER
            format = PixelFormat.TRANSLUCENT
            flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            type=if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                else
                WindowManager.LayoutParams.TYPE_PHONE

        }
        return lp
    }

    //获取视图控件
    fun getView(layoutId:Int): View {
        return View.inflate(mContext,layoutId,null)
    }

    //显示窗口
    fun show(view:View){
        if(view.parent == null){
            vm.addView(view,lp)
        }
    }

    //显示窗口,窗口属性自定义
    fun show(view:View,lp:WindowManager.LayoutParams){
        if(view.parent == null){
            vm.addView(view,lp)
        }
    }

    //隐藏视图
    fun hide(view:View){
        if(view.parent !=null){
            vm.removeView(view)
        }
    }

    //更新视图
    fun update(view:View,lp:WindowManager.LayoutParams){
        vm.updateViewLayout(view,lp)
    }

}